package com.ddtech.business.bank.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ddtech.business.bank.entity.QuestionBankQuestion;
import com.ddtech.business.bank.mapper.QuestionBankQuestionMapper;
import com.ddtech.business.common.BaseService;
import com.ddtech.business.constant.BusinessEnum;
import com.ddtech.exception.BizPreconditions;
import com.ddtech.utils.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 题库题目服务类
 * </p>
 *
 */
@Service
public class QuestionBankQuestionService extends BaseService<QuestionBankQuestionMapper, QuestionBankQuestion> {

    public void doSaveOrUpdate(QuestionBankQuestion questionBankQuestion) {
        questionBankQuestion.init();
        this.saveOrUpdate(questionBankQuestion);
    }

    public void delete(String id) {
        QuestionBankQuestion questionBankQuestion = this.getById(id);
        BizPreconditions.check(questionBankQuestion == null, BusinessEnum.OPERATE_ERROR.getMsg());
        questionBankQuestion.baseDelete();
        this.saveOrUpdate(questionBankQuestion);
    }


    /**
     * description: 根据题库Id查询数据
     * @param:  * @param bankId
     * @param questionDesc
     * @param optionTyp
     * @date: 2023-04-28
     * @return: java.util.List<com.ddtech.business.bank.entity.QuestionBankQuestion>
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    public Map<String,Object> questionBankQuestionPageList(String bankId, String questionDesc, String optionTyp, IPage pageInfo) {
        QueryWrapper<QuestionBankQuestion> queryWrapper = this.newValidQueryWrapper();
        queryWrapper.eq(QuestionBankQuestion.BANK_ID, bankId);
        if(StringUtils.isNotEmpty(questionDesc)){
            queryWrapper.like(QuestionBankQuestion.QUESTION_DESC, questionDesc);
        }
        queryWrapper.eq(QuestionBankQuestion.IS_DELETE, 0);
        if(StringUtils.isNotEmpty(optionTyp)){
            queryWrapper.eq(QuestionBankQuestion.OPTION_TYPE, optionTyp);
        }
        queryWrapper.orderByDesc("update_time");
        IPage<QuestionBankQuestion> page=this.page(pageInfo , queryWrapper);
        //List<QuestionBankQuestion> qBankQuestionList = this.list(queryWrapper);
        //return qBankQuestionList;
        return  PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    /**
     * 根据examId查询QuestionBankQuestion列表
     *
     * @param bankId
     * @return
     */
    public List<QuestionBankQuestion> questionBankQuestionList(String bankId) {
        QueryWrapper<QuestionBankQuestion> queryWrapper = this.newValidQueryWrapper();
        queryWrapper.eq(QuestionBankQuestion.BANK_ID, bankId);
        List<QuestionBankQuestion> qBankQuestionList = this.list(queryWrapper);
        return qBankQuestionList;
    }

    /**
     * 根据题库id批量删除题目
     *
     * @param bankId
     */
    public void batchDeleteQuestionBankQuestion(String bankId) {
        QueryWrapper<QuestionBankQuestion> questionBankQuestionQueryWrapper = this.newValidQueryWrapper();
        questionBankQuestionQueryWrapper.eq(QuestionBankQuestion.BANK_ID, bankId);
        List<QuestionBankQuestion> list = this.list(questionBankQuestionQueryWrapper);
        for (QuestionBankQuestion questionBankQuestion : list) {
            questionBankQuestion.baseDelete();
            this.saveOrUpdate(questionBankQuestion);
        }

    }

    public Map getQueCount(String bankIds) {
        String[] ids = bankIds.split(",");
        QueryWrapper<QuestionBankQuestion> queryWrapper = this.newValidQueryWrapper();
        queryWrapper.in(QuestionBankQuestion.BANK_ID, ids);
        List<QuestionBankQuestion> qBankQuestionList = this.list(queryWrapper);
        Map map = new HashMap();
        map.put("queNum", qBankQuestionList.size());
        return map;
    }
}
